import {EmojiAll} from '@/common/emoji.js'
import {EmojiUrl} from '@/common/chat_config.js'
export default {
	data() {
		return{
			receiveId:null,
			//页面是否已加载
			isReady:false,
			//对话内容
			message:'',
			//输入框中光标位置
			cursor:0,
		}
	},
	computed:{
		receiveUserInfo(){
			return this.$store.state.chat.userInfoObject[this.receiveId]||{}
		},
		chatRecord(){
			return this.$store.state.chat.chatRecord[this.receiveId]||[];
		},
	},
	
	onLoad:function(e){
		if(e.id){
			this.receiveId=e.id
		}else{
			uni.redirectTo({
				url: '/pages/contact/index'
			});
		}
	},
	
	watch:{
		'chatRecord':function(){
			this.$nextTick(function(){
				uni.pageScrollTo({
					 scrollTop: 1000000,
				})
				//this.$set(this.op,'scrollTop',1000000 + Math.random())
			})
		},
		'receiveUserInfo':function(value){
			uni.setNavigationBarTitle({
				title:value.nickname||''
			})
		},
		'message':function(value){
			this.hasMeaage=value.length>0			
		}
	},
	onShow:function(){},
	methods:{
		init(){
			let id =this.receiveUserInfo.id,data={ online:[ this.receiveId ]}
			//取得聊天对象用户信息
			if(!id){	
				let tpl=JSON.stringify({controller:'User',method:'getUserList',data})
				uni.sendSocketMessage({
					data:tpl
				});
			}
			//取得聊天记录
			let send=this.userInfo.id,
			receive=this.receiveId;
			data = JSON.stringify({controller:'PrivateChat',method:'getRecord','data':{send,receive}});
			uni.sendSocketMessage({data});
			
			//请空本地末读消息
			this.$store.commit('chat/markRead',receive)
		},
		send(data,type='text'){			
			let send=this.userInfo.id,
				receive=this.receiveUserInfo.id;
				data={...data,send,receive,type};
			/* 传送数据 */
			data=JSON.stringify({controller:'PrivateChat',method:'send',data})
			uni.sendSocketMessage({data});
			//this.$set(this.op,'scrollTop',1000000 + Math.random())
		},
		sendMeaage(){
			this.hideEmoji()
			this.hideTools()
			if(this.message===''){return;}
			setTimeout(()=>{
				let message=this.message
				this.message=''
				message = this.replaceEmoji(message);
				this.send({message})
			},100)			
		},
		//选择文件
		uploadFile(data){
			this.hideEmoji()
			this.hideTools()
			const  {url,width,height}=data
			switch(data.type){
				case 'image':
					this.send({url,width,height},data.type)
					break;
				case 'video':
					this.send({url,width,height},data.type)
					break;
				default:
			}
		},
		//输入框失焦
		textareaBlurEvent(event){
			this.message =event.detail.value
			this.cursor= event.detail.cursor||0;
		},
		//选取表情包
		checkEmoji(item){
			let cursor =this.cursor,text=this.message,start,end
			this.cursor=cursor+item.alt.length
			if(cursor === 0){
				this.message = item.alt +text;
				return;
			}
			start=text.substring(0,cursor)
			end=text.substr(cursor)
			this.message=start+item.alt+end;
			
		},
		//替换表情包
		replaceEmoji(str){
			let replacedStr = str.replace(/\[([^(\]|\[)]*)\]/g,(item)=>{					
				for (let i=0;i<EmojiAll.length;i++) {
					let EM = EmojiAll[i];
					if(EM.alt==item){
						// 一定要修改为你自己的图床。
						let imgstr = "<img src='"+EmojiUrl+EM.url+"'>";
						return imgstr;
					}
				}
			});
			//t-e= talk-emoji
			return "<div class='t-e'>"+replacedStr+"</div>";
		},
	}
	
}